<?php

# http://projects:8081/cdapp-2kt-api/api/utils_flow_tma_update.php

include_once '_portal.php';

$tags = explode(':', param('NTFM_AP_PAYLOAD')['ZUUU']['key']);

$len = count($tags);

$i = 0;
$j = 1000;
$c = 0;

for ($i; $i < 999999; $i += $j) {
  $o          = $i == 0 ? 0 : ($i - 1);
  $sql        = "SELECT * FROM `2kt_flow_record_airports` WHERE `item` = 'ZUUU' LIMIT {$o}, {$j}";
  $sql_update = [];
  $data       = query_parse(exec_sql($sql));

  if ($data && $data != null) {
    foreach ($data as $line) {
      if (count($line['content']) != $len) {
        // output($line);
        array_splice($line['content'], 3, 0, 0);
        array_splice($line['history'], 3, 0, 0);
        $k            = $line['mixKey'];
        $con          = json_encode($line['content']);
        $his          = json_encode($line['history']);
        $sql_update[] = "UPDATE `2kt_flow_record_airports` SET `content` = '{$con}', `history` = '{$his}' WHERE `mixKey` = '{$k}'";
        $c++;
      }
    }
  } else {
    $i = 999999;
  }
  output(['result' => exec_sql_array($sql_update)]);
  echo PHP_EOL . PHP_EOL . $c . PHP_EOL . PHP_EOL;
}

die();